package week13.topic05;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.component.pulsar.PulsarComponent;
import org.apache.camel.component.pulsar.PulsarConfiguration;
import org.apache.camel.component.rabbitmq.RabbitMQComponent;
import org.apache.camel.impl.DefaultCamelContext;

import javax.jms.ConnectionFactory;

public class TestCamel {


    public static void main(String[] args) throws Exception {
        CamelContext context = new DefaultCamelContext(); // 1. 创建 CamelContext.

        ConnectionFactory activemqConnectionfactory =
                new ActiveMQConnectionFactory("admin", "admin", "tcp://localhost:61616");


        RabbitMQComponent rabbitmqComponent = new RabbitMQComponent();
        rabbitmqComponent.setHostname("127.0.0.1");
        rabbitmqComponent.setPortNumber(5672);
        rabbitmqComponent.setUsername("admin");
        rabbitmqComponent.setPassword("admin");

        PulsarComponent pulsarComponent = new PulsarComponent();
        PulsarConfiguration pulsarConfiguration = new PulsarConfiguration();
        pulsarConfiguration.setServiceUrl("pulsar://192.168.75.131:6650");
        pulsarComponent.setConfiguration(pulsarConfiguration);
        //添加组件标签
        context.addComponent("activemq", JmsComponent.jmsComponentAutoAcknowledge(activemqConnectionfactory));
        context.addComponent("rabbitmq",rabbitmqComponent);
        context.addComponent("pulsar",pulsarComponent);


        //产生数据的逻辑处理
        final TestMessage message = new TestMessage();

        context.addRoutes(new RouteBuilder() {
            public void configure() {
                //产生消息,也可以用另外的生产者产生消息
                from("timer://foo?fixedRate=true&period=1s&repeatCount=10").
                        process(message).to("activemq:queue:week13.topic05");

                //activemq接收消息之后发送rabbitmq
                from("activemq:queue:week13.topic05").to("rabbitmq:exchangeName?queue=week13.topic05");
                //from("activemq:queue:week13.topic05").to("pulsar:persistent//public/default/my-topic");
                //rabbit接收到消息后发送给pulsar
                from("rabbitmq:exchangeName?queue=week13.topic05").to("pulsar:persistent//public/default/my-topic");
                //from("activemq:topic:week13.topic05").to("activemq:queue:testQueue");
            }
        });

        // 3. 添加路由到CamelContext
        context.setTracing(true);
        context.start(); // 4. 启动CamelContext.
        Thread.sleep(Long.MAX_VALUE);  // 为了保持CamelContext处于工作状态，这里需要sleep主线程
        context.stop(); // 最后停止CamelContext
    }
}